﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TinyCinema.Data;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using TinyCinema.Bussiness;

namespace TinyCinema.Bussiness.Cinema
{
    public class DeskType : DependencyObject
    {
        public static DependencyProperty lstDeskTypeProperty = DependencyProperty.Register("lstDeskType", typeof(ObservableCollection<Data.DeskType>), typeof(DeskType));
        public ObservableCollection<Data.DeskType> lstDeskType
        {
            get { return (ObservableCollection<Data.DeskType>)GetValue(lstDeskTypeProperty); }
            set
            {
                SetValue(lstDeskTypeProperty, value);
            }
        }
        public static DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(DeskType));
        public int SelectedIndex
        {
            get { return (int)GetValue(SelectedIndexProperty); }
            set
            {
                SetValue(SelectedIndexProperty, value);
            }
        }
        public static DependencyProperty IsViewingProperty = DependencyProperty.Register("IsViewing", typeof(bool), typeof(DeskType));
        public bool IsViewing
        {
            get { return (bool)GetValue(IsViewingProperty); }
            set
            {
                SetValue(IsViewingProperty, value);
            }
        }
        public Messenger Messenger { get; set; }
        public ICommand CreateCommand { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand SaveCommand { get; set; }
        public DeskType()
        {            
            Messenger = new Bussiness.Messenger();
            Data.DeskType.lstType = Data.DeskType.GetAllDeskType();
            lstDeskType = Data.DeskType.lstType;
            CreateCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    if (lstDeskType == null)
                        lstDeskType = new ObservableCollection<Data.DeskType>();
                    lstDeskType.Add(new Data.DeskType());
                });
            SaveCommand = new RelayCommand<Data.DeskType>(
                 (s) => true,
                 (s) =>
                 {

                     if (s != null)
                     {
                         try { s.Save(); }
                         catch { Data.Execution.Restore(); }
                     }
                 });
            DeleteCommand = new RelayCommand<object>(
                (s) => true,
                (s) =>
                {
                    var message1 = new Message.DeleteConfirmation();
                    Messenger.Send(message1);
                    if (message1.Result == MessageBoxResult.Yes)
                    {
                        try
                        {
                            lstDeskType[SelectedIndex].Delete();
                            lstDeskType.RemoveAt(SelectedIndex);
                        }
                        catch
                        {
                            var message2 = new Message.DeleteDeskTypeFailed();
                            Messenger.Send(message2);
                            Data.Execution.Restore();
                        }

                    }
                });
        }
    }
}
